<script setup>
import {onMounted,reactive,ref} from "vue";
import {httpGet,httpPost} from "@/utils/http.js";
// 图片压缩
import Compressor from "compressorjs";
// 拖拽组件
import {Sortable} from "sortablejs";
import {ElMessage} from "element-plus";
import {dateFormat,removeArrayItem} from "@/utils/libs.js";
import { InfoFilled, Plus, UploadFilled } from "@element-plus/icons-vue";

// 变量定义
const items = ref([])
const item = ref({})
const showDialog = ref(false)
const title = ref('')
const rules = reactive({
  name: [{required: true, message: '请输入菜单名称', trigger: 'change',}],
  icon: [{required: true, message: '请上传菜单图标', trigger: 'change',}],
  url: [{required: true, message: '请输入菜单地址', trigger: 'change',}],
})

const loading = ref(true)
const formRef = ref(null)

// 查询菜单数据
const fetchData = () => {
  loading.value = true
  httpGet('api/admin/menu/list').then(res => {
    if(res.data) {
      const arr = res.data
      // 对日期进行格式化
      for (let i = 0; i < arr.length; i++) {
        arr[i].last_used_at = dateFormat(arr[i].last_used_at)
      }
      items.value = arr
    }
    loading.value = false
  }).catch(e => {
    loading.value = false
    ElMessage.error("获取菜单数据失败："+e.message)
  })
}

onMounted(() => {
  // 选择标签
  const drawBodyWrapper = document.querySelector('.el-table__body tbody')
  fetchData()
  // 初始化拖动排序插件
  Sortable.create(drawBodyWrapper,{
    sort: true, // 允许列表项在容器内拖拽排序
    animation: 500,  // 拖拽动画时长为500ms，提升视觉流畅度
    // 拖拽结束时的回调函数，参数包含新旧索引和来源容器
    // newIndex含义：拖拽结束后元素的新位置索引（从0开始）。比如：将第2行拖到第4行，则 newIndex 为3（索引从0开始）
    // oldIndex含义：拖拽前元素的原始位置索引。比如：初始在第2行，则 oldIndex 为1。
    // from含义：拖拽发生的父容器 DOM 元素，此处为 tbody 元素。用于获取所有子元素
    onEnd({newIndex,oldIndex,from}) {
      // 拖拽位置没有改变时跳过
      if (oldIndex === newIndex) {
        return
      }
      // Array.from(from.children)作用：将拖拽后的 DOM 子元素转换为数组，遍历获取每行的 data-id 属性
      // row.querySelector('.sort')作用：从每行中查找类名为 sort 的元素，假设该元素存储了数据唯一标识（如 <div class="sort" data-id="5"></div>）。
      // sortedData 中储存的就是每条数据的id
      const sortedData = Array.from(from.children).map(row => row.querySelector('.sort').getAttribute('data-id'))
      const ids = []
      const sorts = []
      // 遍历 sortedData 拿到 id 和 排序序号
      sortedData.forEach((id,index) => {
        ids.push(parseInt(id))
        sorts.push(index + 1)
        // 重新设置列表数据的排序字段值
        items.value[index].sort_num = index + 1
      })
      httpPost('api/admin/menu/sort',{ids: ids,sorts: sorts}).catch(e => {
        ElMessage.error('排序失败：'+e.message)
      })
    }
  })
})

// 点击修改触发
const add = () => {
  title.value = '新增菜单'
  showDialog.value = true
  item.value = {}
}

// 点击编辑触发
const edit = (row) => {
  title.value = '修改菜单'
  showDialog.value = true
  item.value = row
}

// 添加或者修改菜单
const save = () => {
  formRef.value.validate((valid) => {
    if(valid) {
      // 判断是否有Id，如果有Id 就表示是修改，如果没有Id表示添加，我们就设置添加数据的排序
      if(!item.value.id) {
        item.value.sort_num = items.value.length + 1
      }
      httpPost('api/admin/menu/saveOrUpdate',item.value).then(() => {
        ElMessage.success('操作成功！')
        fetchData()
        showDialog.value = false
      }).catch(e => {
        ElMessage.error('操作失败：'+e.message)
      })
    }else{
      return false
    }
  })
}

// 启用|禁用
const enable = (row) => {
  httpPost('api/admin/menu/enable',{id: row.id,enabled: row.enabled}).then(() => {
    ElMessage.success('操作成功！')
  }).catch(e => {
    ElMessage.error('操作失败：'+e.message)
  })
}

// 删除菜单
const remove = (row) => {
  httpGet('api/admin/menu/remove?id='+row.id).then(res => {
    ElMessage.success('删除成功！')
    items.value = removeArrayItem(items.value,row,(v1,v2) => {
      return v1.id === v2.id
    })
  }).catch(e => {
    ElMessage.error('删除失败：'+e.message)
  })
}

// 图片上传
const uploadImg = (file) => {
  // 压缩图片并上传
  new Compressor(file.file,{
    // 压缩质量, 0-1
    quality: 0.6,
    // success 表示压缩成功，result 表示压缩成功之后的文件
    success(result) {
      const formData = new FormData()
      formData.append('file',result,result.name)
      // 执行上传操作
      httpPost('api/admin/file/upload',formData).then((res) => {
        item.value.icon = res.data.url
        ElMessage.success('上传成功')
      }).catch(e => {
        ElMessage.error('上传失败：'+e.message)
      })
    },
    error(e) {
      ElMessage.error('图片压缩失败：'+e.message)
    }
  })
}

</script>

<template>
  <div class="container menu" v-loading="loading">
    <div class="handle-box" style="margin-bottom: 10px">
      <el-button type="primary" :icon="Plus" @click="add">新增</el-button>
    </div>
    <el-row>
      <el-table :data="items" :row-key="row => row.id" table-layout="auto" border style="width: 100%">
        <el-table-column prop="name" label="菜单名称">
          <template #default="scope">
            <span class="sort" :data-id="scope.row.id">
              <i class="iconfont icon-drag"></i>
              {{ scope.row.name }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="icon" label="菜单图标">
          <template #default="scope">
            <span v-if="scope.row.icon.startsWith('icon')">
              <i class="iconfont" :class="scope.row.icon" style="font-size: 30px"></i>
            </span>
            <el-image class="menu-icon" :src="scope.row.icon" v-else/>
          </template>
        </el-table-column>
        <el-table-column prop="url" label="菜单URL"/>
        <el-table-column prop="enabled" label="启用状态">
          <template #default="scope">
            <el-switch v-model="scope.row['enabled']" @change="enable(scope.row)" />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" >
          <template #default="scope">
            <el-button size="small" type="primary" @click="edit(scope.row)">编辑</el-button>
            <el-popconfirm title="确定要删除当前记录吗?" @confirm="remove(scope.row)" :width="200">
              <template #reference>
                <el-button size="small" type="danger">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-dialog v-model="showDialog" :title="title" :close-on-click-modal="false">
      <el-form :model="item" label-width="120px" ref="formRef" :rules="rules">
        <el-form-item label="菜单名称：" prop="name">
          <el-input v-model="item.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="菜单图标：" prop="icon">
          <template #label>
            <div class="label-title">
              菜单图标
              <el-tooltip effect="dark" content="可以填写 iconfont 图标名称也可以自己上传图片" raw-content placement="right">
                <el-icon>
                  <InfoFilled />
                </el-icon>
              </el-tooltip>
            </div>
          </template>
          <el-input v-model="item.icon" placeholder="菜单图标地址">
            <template #append>
              <el-upload :auto-upload="true" :show-file-list="false" :http-request="uploadImg">
                <el-icon class="uploader-icon">
                  <UploadFilled/>
                </el-icon>
              </el-upload>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="菜单URL：" prop="url">
          <el-input v-model="item.url" autocomplete="off" />
        </el-form-item>
        <el-form-item label="启用状态：" prop="enable">
          <el-switch v-model="item.enabled" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="save">提交</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<style lang="stylus">
@import "../../assets/css/admin/form.styl"
@import "../../assets/css/main.styl"
.menu {

  .handle-box {
    margin-bottom 20px
  }
  .menu-icon {
    width 36px
    height 36px
  }

  .sort {
    cursor move
    .iconfont {
      position relative
      top 1px
    }
  }

  .el-select {
    width: 100%
  }

  .el-table .cell {
    height: 100% !important;
    line-height: 1 !important;
  }
}
</style>